//
//  MeInfoView.swift
//  whale
//
//  Created by topnet on 2022/7/1.
//

import UIKit

class MeInfoView: ICBaseView {

    override func _initUI() {
        avater = UIImageView()
        avater.contentMode = .scaleAspectFill
        avater.layer.cornerRadius = 30.adapter
        avater.layer.masksToBounds = true
        
        nameLbl = UILabel()
        nameLbl.font = .boldSystemFont(ofSize: 30)
        nameLbl.textColor = .title
        
        desLbl = UILabel()
        desLbl.font = .systemFont(ofSize: 15)
        desLbl.textColor = .content
        
        
        
        da.addSubviews(sub: [avater, nameLbl, desLbl])
        lastV = avater
        autoBottomInset = 20.adapter
        initFrame()
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(actionLogin))
        addGestureRecognizer(tap)
    }
    
    @objc private func actionLogin() {
        if WitWork.share.user != nil {
            return
        }

        guard let email = WitWork.share.user?.email,
              email.validateEmailString() == true else {
            let authenVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AuthenNavigation") as! NavigationViewController
            authenVC.modalPresentationStyle = .formSheet
            if let loginVC = authenVC.viewControllers.first as? LoginView {
                loginVC.completion = { finished in
                    (self.da.vc as? MeVC)?.reloadData()
                    self.da.vc?.dismiss(animated: true, completion: nil)
                }
            }
            self.da.vc?.present(authenVC, animated: true, completion: nil)
            return
        }
    }
    
    private func setupData() {
        let userDefault = UserDefaults.standard
        let selected = userDefault.integer(forKey: "selected")
        var isBuy : Bool = false
        
        if selected != 0 {
            isBuy = userDefault.bool(forKey: "isBuy")
        }
        
    
        if isBuy {
            if let date = UserDefaults.standard.value(forKey: "transactionDate") as? Date {
                let formatter3 = DateFormatter()
                formatter3.dateFormat = "YYYY年MM月dd日开始"
                desLbl.text = " \(formatter3.string(from: date))"
            }else {
                desLbl.text = WitWork.share.user?.email
            }
            nameLbl.text = "高级会员"
        }
        else {
            nameLbl.text = "普通用户"
            desLbl.text = WitWork.share.user?.email
        }
    }
    
    override func _initData() {
        avater.image = UIImage(named: Bundle.main.icon)
        if WitWork.share.user == nil {
            nameLbl.text = "登录"
            desLbl.text = ""
        }else {
            setupData()
        }
        
    }
    
    func updateData() {
        _initData()
    }
    
    override func initFrame() {
        avater.left(50.adapter).width_height(60.adapter).top(20.adapter+UIScreen.safeTop)
        nameLbl.top(avater.frame.minY).bottom(avater.frame.midY).left(avater.frame.maxX + 10.adapter).right(UIScreen.width-50.adapter)
        desLbl.left_right_height_offsetTop(nameLbl)
        autoHeight()
    }
    
    private var avater: UIImageView!
    private var nameLbl: UILabel!
    private var desLbl: UILabel!
    
}
